﻿Option Explicit On

Public Class frmTransactionEdit

    Private m_TransID As Integer = -1
    Private m_TransChanged As Boolean = False

    Public Sub OpenTransactionEdit(ByVal TransID As Integer)

        m_TransID = TransID

        TransactionsInitialize()

        LoadTransID()

        Me.ShowDialog()

        'TODO: Update row

    End Sub

    Private Function LoadTransID() As Boolean

        Dim cn As New SQLite.SQLiteConnection ' ADODB.Connection()
        Dim da As New SQLite.SQLiteDataAdapter ' Dim rs As New ADODB.Recordset()
        Dim ds As New DataSet
        Dim sSQL As String
        Dim bReturnValue As Boolean = False

        '** Open connection
        cn.ConnectionString = fGetConnectionString()
        cn.Open()

        sSQL = "SELECT * FROM CHECKINGACCOUNT_V1" _
                & " WHERE TRANSID = " & m_TransID

        da = New SQLite.SQLiteDataAdapter(sSQL, cn)
        da.Fill(ds, "CHECKINGACCOUNT_V1")

        If ds.Tables(0).Rows.Count = 1 Then

            combTransFrom.SetID = ds.Tables(0).Rows(0)("ACCOUNTID")
            txtAmountFrom.Text = ds.Tables(0).Rows(0)("TRANSAMOUNT")
            Select Case ds.Tables(0).Rows(0)("TRANSCODE").ToString
                Case "Withdrawal"
                    rbtnWithdrawal.Checked = True
                    combTransTo.SetID = ds.Tables(0).Rows(0)("PAYEEID")
                Case "Deposit"
                    rbtnDeposit.Checked = True
                    combTransTo.SetID = ds.Tables(0).Rows(0)("PAYEEID")
                Case "Transfer"
                    rbtnTransfer.Checked = True
                    combTransTo.SetID = ds.Tables(0).Rows(0)("TOACCOUNTID")
                    txtAmountTo.Text = ds.Tables(0).Rows(0)("TOTRANSAMOUNT")
                    If txtAmountFrom.Text <> txtAmountTo.Text Then
                        lblAmountTo.Visible = True
                        txtAmountTo.Visible = True
                        lblTransExchangeRate.Visible = True
                        txtTransExchangeRate.Visible = True
                        txtTransExchangeRate.Text = txtAmountTo.Text / txtAmountFrom.Text
                    End If
            End Select

            txtTransNumber.Text = ds.Tables(0).Rows(0)("TRANSACTIONNUMBER")
            txtTransComment.Text = ds.Tables(0).Rows(0)("NOTES")
            combTransCategory.SetID = ds.Tables(0).Rows(0)("CATEGID")
            combTransCategorySub.SetID = ds.Tables(0).Rows(0)("SUBCATEGID")
            dtpTransDate.Value = ds.Tables(0).Rows(0)("TRANSDATE")

            bReturnValue = True

        End If

        '*// Close connection
        cn.Close()

        m_TransChanged = False

        Return bReturnValue
    End Function

    Private Sub TransactionsInitialize()
        If m_TransID = -1 Then
            '*// Radio buttons: Default value
            rbtnWithdrawal.Checked = True
        End If

        '*// Fill Comboboxes
        LoadcombTransFrom()
        LoadcombTransTo()
        LoadcombTransCategory()
    End Sub

    Private Sub frmTransactionEdit_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = Not ClosingForm()
    End Sub

    Private Sub frmTransactionEdit_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        'TODO: Remove this line(s), only needed for initial testing
        'OpenTransactionEdit(3502)
        'OpenTransactionEdit(3473)

    End Sub

    Private Sub LoadcombTransFrom()
        Dim sSQL As String

        Try

            '*// Transfer From (Account)
            sSQL = "SELECT ACCOUNTID, ACCOUNTNAME" _
                & " FROM ACCOUNTLIST_V1" _
                & " WHERE Status = 'Open'" _
                    & " AND ACCOUNTTYPE = 'Checking'" _
                & " ORDER BY ACCOUNTNAME"
            combTransFrom.fillData(sSQL)

        Catch ex As Exception
            mdlLOGGING.fLOG("frmTransactionEdit => LoadcombTransFrom: " & ex.Message.ToString, 99)
        End Try
    End Sub

    Private Sub LoadcombTransTo()
        Dim sSQL As String

        Try

            Select Case True
                Case rbtnWithdrawal.Checked, rbtnDeposit.Checked
                    sSQL = "SELECT PAYEEID, PAYEENAME" _
                        & " FROM PAYEE_V1" _
                        & " ORDER BY PAYEENAME"
                    lblTransTo.Text = "Payee"

                    'Case rbtnDeposit.Checked
                    '    sSQL = "SELECT PAYEEID, PAYEENAME" _
                    '        & " FROM PAYEE_V1" _
                    '        & " ORDER BY PAYEENAME"
                    '    lblTransTo.Text = "Payee"

                Case rbtnTransfer.Checked
                    '*// Transfer To (Account)
                    sSQL = "SELECT ACCOUNTID, ACCOUNTNAME" _
                        & " FROM ACCOUNTLIST_V1" _
                        & " WHERE Status = 'Open'" _
                            & " AND ACCOUNTTYPE = 'Checking'" _
                        & " ORDER BY ACCOUNTNAME"
                    lblTransTo.Text = "To"

                Case Else
                    '*// ERROR
                    sSQL = "SELECT ACCOUNTID, ACCOUNTNAME" _
                        & " FROM ACCOUNTLIST_V1" _
                        & " WHERE Status = 'Error'"
                    lblTransTo.Text = "Error"
            End Select
            '*// Empty choosen selection
            combTransTo.SelectedIndex = -1

            Application.DoEvents()

            combTransTo.fillData(sSQL)

        Catch ex As Exception
            mdlLOGGING.fLOG("frmTransactionEdit => LoadcombTransTo: " & ex.Message.ToString, 99)
        End Try
    End Sub

    Private Sub LoadcombTransCategory()
        Dim sSQL As String

        Try

            '*// Category
            sSQL = "SELECT CATEGID, CATEGNAME" _
                & " FROM CATEGORY_V1" _
                & " ORDER BY CATEGNAME"
            combTransCategory.fillData(sSQL)

            LoadcombTransCategorySub(-1)

        Catch ex As Exception
            mdlLOGGING.fLOG("frmTransactionEdit => LoadcombTransCategory: " & ex.Message.ToString, 99)
        End Try
    End Sub

    Private Sub LoadcombTransCategorySub(ByVal CategID As Integer)
        Dim sSQL As String

        Try

            '*// Sub Category
            sSQL = "SELECT SUBCATEGID, SUBCATEGNAME" _
                & " FROM SUBCATEGORY_V1" _
                & " WHERE CATEGID = " & CategID _
                & " ORDER BY SUBCATEGNAME"
            combTransCategorySub.fillData(sSQL)

            combTransCategorySub.SelectedIndex = -1
            combTransCategorySub.Text = ""

        Catch ex As Exception
            mdlLOGGING.fLOG("frmTransactionEdit => LoadcombTransCategory: " & ex.Message.ToString, 99)
        End Try
    End Sub

    Private Function ClosingForm() As Boolean
        If m_TransChanged Then
            If MsgBox("Transaction has been changed, when you close this transaction, your changes will be lost, Continue closing form?", vbInformation + MsgBoxStyle.YesNo, Application.ProductName) = MsgBoxResult.Yes Then
                'Me.Close()
                Return True
            End If

        Else
            'Me.Close()
            Return True
        End If

        Return False

    End Function

    Private Sub btnTransCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnTransCancel.Click
        Me.Close()
    End Sub

    Private Sub btnTransSave_Click(sender As System.Object, e As System.EventArgs) Handles btnTransSave.Click

    End Sub

    Private Sub combTransCategory_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles combTransCategory.SelectedIndexChanged
        m_TransChanged = True
    End Sub

    Private Sub combTransCategory_SelectedValueChanged(sender As Object, e As System.EventArgs) Handles combTransCategory.SelectedValueChanged

        LoadcombTransCategorySub(combTransCategory.getSelectedID)

    End Sub


    Private Sub rbtnWithdrawal_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbtnWithdrawal.CheckedChanged
        LoadcombTransTo()
        m_TransChanged = True
    End Sub

    Private Sub rbtnDeposit_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbtnDeposit.CheckedChanged
        LoadcombTransTo()
        m_TransChanged = True
    End Sub

    Private Sub rbtnTransfer_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbtnTransfer.CheckedChanged
        LoadcombTransTo()
        m_TransChanged = True
    End Sub

    Private Sub combTransFrom_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles combTransFrom.Validating
        If combTransFrom.SelectedIndex = -1 Then
            'e.Cancel = True
        End If
    End Sub

    Private Sub combTransFrom_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles combTransFrom.SelectedIndexChanged
        m_TransChanged = True
    End Sub

    Private Sub combTransTo_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles combTransTo.SelectedIndexChanged
        m_TransChanged = True
    End Sub

    Private Sub combTransCategorySub_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles combTransCategorySub.SelectedIndexChanged
        m_TransChanged = True
    End Sub

    Private Sub txtTransComment_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtTransComment.TextChanged
        m_TransChanged = True
    End Sub

    Private Sub dtpTransDate_ValueChanged(sender As System.Object, e As System.EventArgs) Handles dtpTransDate.ValueChanged
        m_TransChanged = True
    End Sub

    Private Sub txtTransNumber_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtTransNumber.TextChanged
        m_TransChanged = True
    End Sub

    Private Sub txtAmountFrom_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtAmountFrom.TextChanged
        m_TransChanged = True
    End Sub

    Private Sub txtAmountTo_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtAmountTo.TextChanged
        m_TransChanged = True
    End Sub

    Private Sub btnTransSplit_Click(sender As System.Object, e As System.EventArgs) Handles btnTransSplit.Click
        'TODO: Create Split Screen
    End Sub
End Class